package com.project.controller;

import com.framework.core.result.APIObjResponse;
import com.project.biz.I18nBiz;
import com.project.util.ImgUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 图片上传
 * @Author: wangzhiqiang
 * @CreateDate: 2020-03-25 16:52
 * @Version: 1.0
 */
@Api(value = "ImgUploadController", description = "图片上传")
@Slf4j
@RestController
public class ImgUploadController {

    /**
     * 图片上传帮助类
     */
    @Autowired
    private ImgUtil imgUtil;

    /**
     * 头像保存地址
     */
    @Value("${img.portraitImgAddress}")
    private String portraitImgAddress;

    /**
     * 头像访问
     */
    @Value("${img.portraitImgShowAddress}")
    private String portraitImgShowAddress;

    /**
     * 商品图片保存地址
     */
    @Value("${img.productImgAddress}")
    private String productImgAddress;

    /**
     * 商品图片访问地址
     */
    @Value("${img.productImgShowAddress}")
    private String productImgShowAddress;

    /**
     * 商品评价图片保存地址
     */
    @Value("${img.productEvaImgAddress}")
    private String productEvaImgAddress;

    /**
     * 商品评价图片访问地址
     */
    @Value("${img.productEvaImgShowAddress}")
    private String productEvaImgShowAddress;

    /**
     * 国际化
     */
    @Autowired
    private I18nBiz i18nBiz;

    /**
     * 上传头像
     * @param file
     * @return
     */
    @ApiOperation(value = "上传头像")
    @PostMapping(value = "/uploadPortraitImg")
    public APIObjResponse<String> uploadPortraitImg(@RequestParam("file") MultipartFile file){
        //图片按天分组
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        String day = dateFormat.format(new Date());
        String dir = portraitImgAddress + day + "/";
        String url = imgUtil.uploadImg(file, dir);

        String returnUrl = portraitImgShowAddress + day + "/" + url;
        log.info("图片上传成功，访问路径:" + returnUrl);
        return APIObjResponse.success(this.i18nBiz.getMessage("msg.successfullyUploaded"), returnUrl);
    }

    /**
     * 上传商品图片
     * @param file
     * @return
     */
    @ApiOperation(value = "上传商品图片")
    @PostMapping(value = "/uploadProductImg")
    public APIObjResponse<String> uploadTaskImg(@RequestParam("file") MultipartFile file){
        //图片按天分组
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        String day = dateFormat.format(new Date());
        String dir = productImgAddress + day + "/";
        String url = imgUtil.uploadImg(file, dir);

        String returnUrl = productImgShowAddress + day + "/" + url;
        log.info("图片上传成功，访问路径:" + returnUrl);
        return APIObjResponse.success(this.i18nBiz.getMessage("msg.successfullyUploaded"), returnUrl);
    }

    /**
     * 上传商品评论图片
     * @param file
     * @return
     */
    @ApiOperation(value = "上传商品评论图片")
    @PostMapping(value = "/uploadProductEvaImg")
    public APIObjResponse<String> uploadProductEvaImg(@RequestParam("file") MultipartFile file){
        //图片按天分组
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        String day = dateFormat.format(new Date());
        String dir = productEvaImgAddress + day + "/";
        String url = imgUtil.uploadImg(file, dir);

        String returnUrl = productEvaImgShowAddress + day + "/" + url;
        log.info("图片上传成功，访问路径:" + returnUrl);
        return APIObjResponse.success(this.i18nBiz.getMessage("msg.successfullyUploaded"), returnUrl);
    }

}
